package com.asset.dispoal.evidenceType.service.commom;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.evidenceType.domain.db.entity.BsEvidenceType;
import com.asset.dispoal.evidenceType.domain.db.mapper.BsEvidenceTypeMapper;
import com.asset.dispoal.evidenceType.dto.BsEvidenceTypeRequest;
import com.asset.dispoal.evidenceType.dto.BsEvidenceTypeResponse;
import com.asset.dispoal.evidenceType.service.converter.EvidenceTypeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/24$
 */
@Component
public class EvidenceTypeQueryCommand implements BaseCommand<BsEvidenceTypeRequest, Result> {


    @Autowired
    BsEvidenceTypeMapper bsEvidenceTypeMapper;


    @Override
    public Result<PageVO<BsEvidenceTypeResponse>> execute(BsEvidenceTypeRequest request) {
        BsEvidenceType bsEvidenceType = EvidenceTypeConverter.INSTANCE.reqToDo(request);
        bsEvidenceType.setPermission((String) GlobalHeaderThreadLocal.getOrException().getExtraMap().get("permission"));
        int count = bsEvidenceTypeMapper.count(bsEvidenceType);
        Page<BsEvidenceType> page = PagingQuery.getPage(count, bsEvidenceType);
        PageVO<BsEvidenceTypeResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<BsEvidenceType> bsEvidenceTypes = bsEvidenceTypeMapper.page(page);
            List<BsEvidenceTypeResponse> bsEvidenceTypeResponses = EvidenceTypeConverter.INSTANCE.dosToRes(bsEvidenceTypes);
            pageVO.setResult(bsEvidenceTypeResponses);
        }
        return Result.ok(pageVO);
    }
}
